package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.dto.ResGroup;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.service.ResGroupService;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.entity.GroupClassStateParameter;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @description:当前状态同步-头盔课程同步状态请求
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Component
@Slf4j
public class CvrSyncStateRequestMessageHandler implements MessageHandler {

    @Autowired
    ResGroupService resGroupService;

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {


        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_CVR_SYNC_STATE_REQUEST)){
            log.info("处理头盔课程同步状态信息");
            BaseResult<ResGroup> result = resGroupService.getGroupClassStateByUdid(message.getCommand().getUdid());

            Action action = new Action();
            action.setAction(MessageActionConstants.MESSAGE_ACTION_CVR_SYNC_STATE_RESPONSE);

            Command<GroupClassStateParameter> command = new Command<>();
            command.setUdid(message.getCommand().getUdid());
            command.setDevId(message.getCommand().getDevId());

            GroupClassStateParameter groupClassStateParameter = new GroupClassStateParameter();

            if(result.isSuccess()){
                ResGroup resGroup = result.getData();

                groupClassStateParameter.setCode(result.getCode());
                groupClassStateParameter.setSuccess(result.isSuccess());
                groupClassStateParameter.setMessage(result.getMessage());
                groupClassStateParameter.setContentId(resGroup.getContentId());
                groupClassStateParameter.setSectionNo(resGroup.getSectionNo());
                groupClassStateParameter.setClassState(resGroup.getClassState());
                groupClassStateParameter.setSoundState(resGroup.getSoundState());
                groupClassStateParameter.setLockState(resGroup.getLockState());
                groupClassStateParameter.setPlayState(resGroup.getPlayState());
                groupClassStateParameter.setTeachState(resGroup.getCtrlModeState());
                groupClassStateParameter.setTimeline(resGroup.getTimeline());
                groupClassStateParameter.setPosition(resGroup.getPosition());

            }else{
                groupClassStateParameter.setCode(result.getCode());
                groupClassStateParameter.setSuccess(result.isSuccess());
                groupClassStateParameter.setMessage(result.getMessage());
            }

            command.setParameter(groupClassStateParameter);
            action.setCommand(command);

            rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
            WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);

        }
    }
}
